package item.dao.Impl;


import commons.pojo.*;
import commons.vo.LivegoodsResult;
import item.dao.ItemDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class ItemDaoImpl implements ItemDao {
    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public LivegoodsResult selectRecommendation(int page, int rows, String tag) {

        Query query = new Query();
        Criteria criteria = new Criteria();
        criteria.andOperator(Criteria.where("tag").is(tag));
        query.addCriteria(Criteria.where("tag").is("其他人民调解案例"));
        query.with(PageRequest.of(0,4));
        System.out.println(Criteria.where("tag").is(tag));
        int count1 = (int) mongoTemplate.count(query, Item.class);
        query.skip((page - 1) * 10L);
        //设置查询条数
        query.limit(10);
        //查询总记录数
        int count = (int) mongoTemplate.count(query, Item.class);
        if (count <= 0) {
            ArrayList<Object> list = new ArrayList<>();
            LivegoodsResult ok = LivegoodsResult.ok(list);
            ok.setResults(count1);
        }

        query.skip((long)(page - 1) * 10)
                .limit(10);
        List<Item> ApplyLogList = mongoTemplate.find(query, Item.class);
        LivegoodsResult ok = LivegoodsResult.ok(ApplyLogList);
        ok.setResults(count1);
        return ok;
    }

    @Override
    public LivegoodsResult selectFaShiPage(int page) {
        Query query = new Query();
//        List<Fashi> all = mongoTemplate.findAll(Fashi.class);

        int count1 = (int) mongoTemplate.count(query, Fashi.class);
        query.skip((page - 1) * 15);
        //设置查询条数
        query.limit(15);
        //查询总记录数
        int count = (int) mongoTemplate.count(query, Fashi.class);

        if (count <= 0) {
            ArrayList<Object> list = new ArrayList<>();
            LivegoodsResult ok = LivegoodsResult.ok(list);
            ok.setResults(count1);
        }

        query.skip((long)(page - 1) * 15)
                .limit(15);
        List<Fashi> ApplyLogList = mongoTemplate.find(query, Fashi.class);
        LivegoodsResult ok = LivegoodsResult.ok(ApplyLogList);
        ok.setResults(count1);
        return ok;
    }

    @Override
    public LivegoodsResult selectFaShi(int id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("sid").is(id));
        List<Fashi> one = mongoTemplate.find(query, Fashi.class);
        return LivegoodsResult.ok(one);
    }

    @Override
    public LivegoodsResult selectHuaTi() {
        List<Huati> all = mongoTemplate.findAll(Huati.class);
        return LivegoodsResult.ok(all);
    }

    @Override
    public LivegoodsResult selectHuaTiId(int id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("sid").is(id));
        List<Huati> one = mongoTemplate.find(query, Huati.class);
        return LivegoodsResult.ok(one);
    }

    @Override
    public LivegoodsResult selectJingXing() {
        List<Jingxing> all = mongoTemplate.findAll(Jingxing.class);
        return LivegoodsResult.ok(all);
    }

    @Override
    public LivegoodsResult selectJingXingId(int id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("sid").is(id));
        List<Jingxing> one = mongoTemplate.find(query, Jingxing.class);
        return LivegoodsResult.ok(one);
    }

    @Override
    public LivegoodsResult seclectShiPin(int page) {
        Query query = new Query();
//        List<Shipin> all = mongoTemplate.findAll(Shipin.class);

        int count1 = (int) mongoTemplate.count(query, Shipin.class);
        query.skip((page - 1) * 12);
        //设置查询条数
        query.limit(12);
        //查询总记录数
        int count = (int) mongoTemplate.count(query, Shipin.class);

        if (count <= 0) {
            ArrayList<Object> list = new ArrayList<>();
            LivegoodsResult ok = LivegoodsResult.ok(list);
            ok.setResults(count1);
        }

        query.skip((long)(page - 1) * 12)
                .limit(12);
        List<Shipin> ApplyLogList = mongoTemplate.find(query, Shipin.class);
        LivegoodsResult ok = LivegoodsResult.ok(ApplyLogList);
        ok.setResults(count1);
        return ok;
    }
}
